/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import OBJECT.Tau;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author ngayngo9x
 */
public class TauDAO1 {
    private Connection con;
    public TauDAO1() {
        con = util.Connection.getConnection();
    }
    
    public int SuaTau(int maTau,String tenTau, int loaiTau, String tinhTrang) {
        System.out.print(" " + maTau + " " + tenTau + " " + loaiTau + " " + tinhTrang);
        CallableStatement cal; 
        try {
            cal = con.prepareCall("{call sp_CapNhatTau(?,?,?,?,?)}"); //con.prepareCall("{call sp_ThemTau(?,?,?)}");
            cal.setInt(1, maTau);
            cal.setString(2, tenTau);
            cal.setInt(3, loaiTau);
            cal.setString(4, tinhTrang);
            cal.registerOutParameter(5, Types.INTEGER);
            cal.setInt(5, loaiTau);
            cal.execute();
            int flag = cal.getInt(5);
            if(flag == 1) {
                return -1;
            } else {
                return 1;
            }
        } catch (SQLException ex) {
            Logger.getLogger(TauDAO1.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    
    public int ThemTau(String tenTau, int loaiTau, String tinhTrang) {
        //Connection con  = util.Connection.getConnection();
        CallableStatement cal; 
        try {
            cal = con.prepareCall("{call sp_ThemTau(?,?,?,?)}"); //con.prepareCall("{call sp_ThemTau(?,?,?)}");
            cal.setString(1, tenTau);
            cal.setInt(2, loaiTau);
            cal.setString(3, tinhTrang);
            cal.registerOutParameter(4, Types.INTEGER);
            cal.setInt(4, loaiTau);
            cal.execute();
            int flag = cal.getInt(4);
            if(flag == 1) {
                return -1;
            } else {
                return 1;
            }
         } catch (SQLException ex) {
            Logger.getLogger(TauDAO1.class.getName()).log(Level.SEVERE, null, ex);
         }
        //System.out.print(tenTau);
        return 0;
    }
    
    public List<Tau> LayDanhSachTau(){
       List<Tau> ds = new ArrayList<Tau>();
       //Connection conn = null;
       CallableStatement cals = null;
       ResultSet rs = null;
       try {
           //con = util.Connection.getConnection();
           cals = this.con.prepareCall("{call sp_ThongTinTau()}");
           rs = cals.executeQuery();
           Tau t = null;
           while(rs.next()){
               t = new Tau();
               t.setMaTau(rs.getInt("Ma_Tau"));
               //System.out.print();
               t.setTenTau(rs.getString("TenTau"));
               t.setLoaiTau(rs.getInt("LoaiTau"));
               t.setTinhTrang(rs.getString("TinhTrangTau"));
               ds.add(t);
           }
       } catch (Exception e) {
           System.err.println(e.getMessage());
       }
       return ds;
   }
    
   public void CloseConnection() {
        try {
            this.con.close();
        } catch (SQLException ex) {
            Logger.getLogger(TauDAO1.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
   
}
